package org.compiere.apps.search;

import java.awt.Frame;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.adempiere.plaf.AdempierePLAF;
import org.compiere.apps.ALayout;
import org.compiere.apps.search.Info;
import org.compiere.apps.search.Info_Column;
import org.compiere.minigrid.IDColumn;
import org.compiere.swing.CLabel;
import org.compiere.swing.CTextField;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class InfoMKCMS extends Info {

	protected InfoMKCMS(Frame frame, boolean modal, int WindowNo, String value, boolean multiSelection, String whereClause) {
		super(frame, modal, WindowNo, "cms_unitmeasure", "cms_unitmeasure_ID", multiSelection, whereClause);
		
		log.info("InfoKPCMS");
		setTitle(Msg.getMsg(Env.getCtx(), "InfoKPCMS"));
		
		try{
			statInit();
			p_loadedOK = initInfo();
		}
		catch (Exception e) {
			return;
		}
		
		int no = p_table.getRowCount();
		setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false);
		setStatusDB(Integer.toString(no));
		if (value != null && value.length() > 0)
		{
			fcode.setValue(value);
			executeQuery();
		}
		//
		pack();
		//	Focus
		fcode.requestFocus();
	}

	private CLabel lname = new CLabel(Msg.translate(Env.getCtx(), "Name"));
	private CTextField fname = new CTextField(20);
	
	private CLabel lname2 = new CLabel(Msg.translate(Env.getCtx(), "Name2"));
	private CTextField fname2 = new CTextField(20);
	
	private CLabel lcode = new CLabel(Msg.translate(Env.getCtx(), "KpvedNum"));
	private CTextField fcode = new CTextField(5);
	
	private void statInit() throws Exception {
		lname.setLabelFor(fname);
		lname2.setLabelFor(lname2);
		lcode.setLabelFor(fcode);
		
		fname.setBackground(AdempierePLAF.getInfoBackground());
		fname.addActionListener(this);
		fname2.setBackground(AdempierePLAF.getInfoBackground());
		fname2.addActionListener(this);
		fcode.setBackground(AdempierePLAF.getInfoBackground());
		fcode.addActionListener(this);
		
		
		parameterPanel.setLayout(new ALayout(2,2,true));
		
		parameterPanel.add(lname);
		parameterPanel.add(fname);
		parameterPanel.add(lname2);
		parameterPanel.add(fname2);
		parameterPanel.add(lcode);
		parameterPanel.add(fcode);
	}
	
	private static final Info_Column[] codes = {
		new Info_Column(Msg.translate(Env.getCtx(), "cms_unitmeasure_id"), "cms_unitmeasure_id", IDColumn.class),
		new Info_Column(Msg.translate(Env.getCtx(), "Name"), "Name", String.class),
		new Info_Column(Msg.translate(Env.getCtx(), "Name2"), "Name2", String.class),
		new Info_Column(Msg.translate(Env.getCtx(), "KpvedNum"), "KpvedNum", String.class)
	};
	
	private boolean initInfo() throws Exception {
		String from = "adempiere.cms_unitmeasure";
		String where = "isActive = 'Y'";
		String orderBy = "KpvedNum";
		
		prepareTable(codes, from, where, orderBy);		
		
		return true;
	}
	
	@Override
	protected String getSQLWhere() {
		
		StringBuffer sql = new StringBuffer();
		
		if(fname.getText().length() > 0)
			sql.append(" AND name like ?");
		if(fname2.getText().length() > 0)
			sql.append(" AND name2 like ?");
		if(fcode.getText().length() > 0)
			sql.append(" AND KpvedNum like ?");

		return sql.toString();
	}

	@Override
	protected void setParameters(PreparedStatement pstmt, boolean forCount) throws SQLException {
		int index = 1;
		
		if(fname.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fname));
		if(fname2.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fname2));
		if(fcode.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fcode));
	}
	
	/**
	 *  Get SQL WHERE parameter
	 *  @param f field
	 *  @return Upper case text with % at the end
	 */
	private String getSQLText (CTextField f)
	{
		String s = f.getText().toUpperCase();
		if (!s.endsWith("%"))
			s = "%"+s+"%";
		log.fine( "String=" + s);
		return s;
	} 

}
